package com.data.action

import com.data.annotation.NoargAnnotation
import com.data.annotation.OpenAnnotation
import com.data.entity.ThemeAdminEntity
import com.data.service.IndexService
import com.data.util.ImageLookUtils
import com.data.util.JsonUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import java.util.*
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


@Controller
@OpenAnnotation
@NoargAnnotation
class IndexAction {

    @Autowired
    private var indexService: IndexService? = null

    /**
     * 进入到首页
     */
    @GetMapping("home")
    fun indexAction(request: HttpServletRequest): String {
        //首页的banner图
        val bannerList = indexService!!.indexBannerService(0)
        request.setAttribute("dataMap", bannerList)
        //首页的案例图
        val listCase = indexService!!.indexCaseService()
        request.setAttribute("dataCase", listCase)
        return "index"
    }

    /**
     * 查看图片
     *
     * @param image
     * @param response
     */
//    @RequestMapping(value = "/image/{type}/{image}", method = [(RequestMethod.GET)])
//    fun showImageAction(@PathVariable("image") image: String, @PathVariable("type") type: Int, response: HttpServletResponse) {
    @GetMapping("image")
    fun showImageAction(image: String, type: Int, response: HttpServletResponse) {
        val inputStream = indexService!!.showImageService(image, type)
        //响应图片
        if (inputStream != null)
            ImageLookUtils.responseImage(inputStream, response)
    }

    /**
     * 案例页面
     */
    @GetMapping("caselist")
    fun indexCaseAction(request: HttpServletRequest): String {
        //案例页面banner图
        val bannerList = indexService!!.indexBannerService(1)
        request.setAttribute("dataMap", bannerList)
        //案例分类
        val listCase = indexService!!.caseClassService()
        request.setAttribute("dataClass", listCase)
        // 案例得主题
        val listTheme = themeCaseAction(0, 10, "0")
        request.setAttribute("dataTheme", listTheme)
        return "caselist"
    }

    /**
     * 主题分类下的主题信息
     */
    @RequestMapping("themeCase")
    @ResponseBody
    fun themeCaseAction(start: Int, end: Int, cid: String): List<ThemeAdminEntity> {
        val num = 10 //默认条数
        return indexService!!.themeCaseService(start, num, cid)
    }

    /**
     * 主题分类下的主题信息
     */
    @RequestMapping("claxxNum")
    @ResponseBody
    fun claxxNumAction(cid: String): Map<String, Any> =
            JsonUtils.returnMassageUtils(true, indexService!!.claxxNumService(cid))


    @GetMapping("love")
    fun loveAction(phone: String, name: String, @RequestParam(required = false) message: String): String {
        val nowday = System.currentTimeMillis()
        val calendar = Calendar.getInstance()
        calendar.set(2018, 2, 14)
        val millis = calendar.timeInMillis
        return if (phone == "13568939633" && name.toUpperCase() == "LL" && nowday >= millis) {
            "love/index"
        } else {
            "redirect:/"
        }


    }

}